<template>
  <div class="shop-car">
    <div class="class-header">
      <div class="header-nav">
        <div class="header-nav-left">
          <router-link to="/">
            <img
              src="../img/logo.png"
              alt
            >
            <span>80教育商城</span>
          </router-link>
        </div>
        <div class="header-nav-right">
          <ul class="header-nav-right-list">
            <li>
              <i class="iconfont icon-search"></i>
            </li>
            <li>
              <span class="search">搜索</span>
            </li>
            <li>
              <span>会员中心</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="shop-car-list-wp">
      <div>
        <div
          class="shop-car-list select-control-row"
          v-for="(lists,index) in list"
          :key='index'
        >
          <div class="shop-car-select">
            <mu-checkbox
              :value="lists.id"
              v-model="selectStatus"
              @change="selectChange"
            ></mu-checkbox>
          </div>
          <div class="shop-car-img">
            <img
              src="../img/book1.jpg"
              alt
            >
          </div>
          <div class="shop-car-listInfo">
            <div class="shop-car-bookname">
              <span>{{lists.goodsname}}</span>
              <span class="iconfont icon-trash" @click="deleteFn(lists.id)"></span>
            </div>
            <div class="shop-car-bookparam">
              <div>
                <span @click="changeNum(2,lists.id,lists.num)">-</span>
                <input
                  type="text"
                  v-model="lists.num"
                  @change="changeNum(1,lists.id,lists.num)"
                >
                <span @click="changeNum(0,lists.id,lists.num)">+</span>
              </div>
              <div class="shop-car-bookprice">￥{{lists.price*lists.num}}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="shop-car-bottom">
      <div class="checkall-wp">
        <div
          class="select-control-row"
          style="padding-left: 5px; line-height: 60px;"
        >
          <mu-checkbox
            label="全选"
            v-model="checkAll"
            @change="handleCheckAll"
          ></mu-checkbox>
        </div>
      </div>
      <div class="shop-car-total-wp">
        <div>
          <span class="shop-car-total-text">总计：</span>
          <span class="shop-car-total-price">￥{{totalPrice}}</span>
        </div>
        <div class="shop-car-sheng">
          <span>已省：</span>
          <span>￥0.00</span>
        </div>
      </div>
      <div class="shop-car-paybtn">
          <button @click="allmony">
            结算
            <span>({{ goodsCount }})</span>
          </button>
      </div>
    </div>
  </div>
</template>

<script>
import session from "../util/session";
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers(
  "shopcar"
);
export default {
  data() {
    return {
      selectStatus: [],
      totalPrice: "0.00",
      checkAll: false,
      goodsCount: 0,
      goodsId:[],
      // value: ""
    };
  },
  computed: {
    ...mapState(["list", "count"])
  },
  methods: {
    ...mapActions(["getlist", "getShopNum", "updateList",'deleteList']),
    deleteFn(id){
        if(confirm('你确定要删除吗？？')){
            this.deleteList(id).then(()=>{
              this.getlist(session.get().id);
          })
        }
    },
    //改变商品数量
    changeNum(add, id, num) {
      let arr = [];
      let count = num;
      if (add == 0) {
        count++;
      } else if (add == 1) {
        if (count <= 1) {
          count = 1;
        }
      } else {
        if (count > 1) {
          count--;
        }
      }
      this.updateList({ id: id, num: count }).then(() => {
        this.getlist(session.get().id);
      });
    },
    //判断商品是否被选中
    selectChange(selArr) {
      let list = this.list;
      let arr = [];
      let total = 0;
      let count = 0;
      let id = [];
      //判断是否全选
      if (this.list.length == this.selectStatus.length) {
        this.checkAll = true;
      } else {
        this.checkAll = false;
      }
      for (var i = 0; i < selArr.length; i++) {
        for (var j = 0; j < list.length; j++) {
          if (selArr[i] == list[j].id) {
            arr.push(list[j]);
          }
        }
      }
      for (var k = 0; k < arr.length; k++) {
        total += arr[k].num * arr[k].price;
        count += arr[k].num * 1;
        id.push(arr[k].goodsid)
      }
      this.goodsId = id
      this.goodsCount = count;
      this.totalPrice = total;
    },
    handleCheckAll(status) {
      if (status) {
        let arr = [];
        for (var i = 0; i < this.list.length; i++) {
          arr.push(this.list[i].id);
        }
        this.selectStatus = arr;
        if (this.list.length == this.selectStatus.length) {
          this.selectChange(this.selectStatus);
        }
      } else {
        this.selectStatus = [];
        this.totalPrice = 0;
        this.goodsCount = 0;
      }
    },
    jian(num) {
      return num <= 1 ? (num = 1) : num--;
    },
    allmony() {
      if(this.selectStatus.length == 0){
        alert('结算不能为空！')
      }else{
        this.$router.push({ name: "ordercommit", params: { id: this.selectStatus } });
      }
    }
  },
  mounted() {
    this.getlist(session.get().id);
  }
};
</script>

<style scoped>
.shop-car {
  max-width: 768px;
  margin-bottom: 50px;
}
.shop-car-bottom {
  position: fixed;
  bottom: 0;
  height: 50px;
  width: 100%;
  max-width: 768px;
  font-size: 15px;
  z-index: 11;
  border-top: 1px solid #ebebeb;
  background-color: #fff;
  display: flex;
}
.shop-car-bottom > div {
  flex-grow: 1;
}
.checkall-wp {
  display: flex;
  line-height: 46px;
}
.checkall {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 0.5px solid #3285ff;
  text-align: center;
  line-height: 17px;
  margin-left: 9px;
  position: relative;
  top: 2px;
}
.icon-right {
  background: #3285ff;
  color: #fff;
}
.checkall-text {
  font-size: 13px;
  color: #666;
  margin-left: 6px;
}
.shop-car-total-wp {
  font-size: 12px;
  color: #333;
  margin-top: 10px;
}
.shop-car-total-wp .shop-car-total-price {
  color: #3285ff;
}
.shop-car-sheng {
  font-size: 10px;
  color: #979797;
  padding-left: 7px;
  margin: 5px 0;
}
.shop-car-paybtn button {
  width: 100%;
  height: 100%;
  background-color: #fff;
  border: none;
  background: -webkit-gradient(
    linear,
    left top,
    right top,
    from(#56c4e7),
    to(#3285ff)
  );
  outline: none;
  font-size: 13px;
  color: #fff;
}
.shop-car-list-wp {
  background-color: #fff;
}
.shop-car-list {
  padding: 10px 0;
  margin: 0 15px 0 6px;
  display: -webkit-box;
}
.shop-car-select {
  width: 30px;
  height: auto;
  line-height: 90px;
  padding-right: 10px;
  font-size: 15px;
  margin-right: 10px;
}
.shop-car-img {
  width: 90px;
  height: 90px;
}
.shop-car-img img {
  width: 100%;
  height: 100%;
}
.shop-car-listInfo {
  flex-grow: 1;
  padding-left: 14px;
}
.shop-car-bookname {
  font-size: 13px;
  color: #333;
  position: relative;
}
.shop-car-bookname .icon-trash {
  position: absolute;
  top: 1px;
  right: -12px;
  font-size: 16px;
}
.shop-car-bookname span {
  height: 16px;
  line-height: 16px;
  margin-right: 5px;
  font-size: 13px;
  color: #666;
  word-break: break-all;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
.shop-car-bookparam {
  display: flex;
  margin-top: 40px;
  position: relative;
}
.shop-car-bookparam > div span {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 1px solid #eee;
  vertical-align: middle;
  text-align: center;
  line-height: 20px;
  color: #949494;
}
.shop-car-bookparam input {
  width: 30px;
  height: 24px;
  outline: none;
  vertical-align: middle;
  text-align: center;
  border: none;
}
.shop-car-bookprice {
  color: #3285ff;
  font-size: 12px;
  float: right;
  width: 38%;
  text-align: right;
  position: absolute;
  top: 6px;
  right: -5px;
}
</style>
